package com.appspot.graph_wave;

import javax.jdo.JDOHelper;
import javax.jdo.PersistenceManager;
import javax.jdo.PersistenceManagerFactory;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Graph {

	private static final PersistenceManagerFactory PMF = JDOHelper
			.getPersistenceManagerFactory("transactions-optional");

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;

	@Persistent
	private String content;

	public Graph(String source) {
		this.content = source;
	}

	public Long getId() {
		return id;
	}

	public String getContent() {
		return content;
	}

	public static Graph addContent(String source) {
		Graph g = new Graph(source);
		PersistenceManager pm = PMF.getPersistenceManager();
		try {
			pm.makePersistent(g);
		} finally {
			pm.close();
		}
		return g;
	}

	public static Graph retrieveContent(Long id) {
		PersistenceManager pm = PMF.getPersistenceManager();
		try {
			return pm.getObjectById(Graph.class, id);
		} finally {
			pm.close();
		}
	}

	public static void deleteContent(Long id) {
		PersistenceManager pm = PMF.getPersistenceManager();
		try {
			pm.deletePersistent(pm.getObjectById(Graph.class, id));
		} finally {
			pm.close();
		}
	}
}
